package com.gz.mylife.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.gz.mylife.R;
import com.gz.mylife.widget.LoadingCover;
import com.gz.mylife.widget.LoadingCover.OnLoadingCoverRefreshListener;

public class BrowserPay extends Activity implements OnLoadingCoverRefreshListener{

	private WebView mWebView;
	private LoadingCover mLoadingCover;
	private TextView img;
	private Dialog mMenudialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_delivery_explainpay);
		mLoadingCover = (LoadingCover) findViewById(R.id.layout_loading_cover);
		mLoadingCover.setOnLoadingCoverRefreshListener(this);
		mWebView = (WebView) findViewById(R.id.webview);
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.getSettings().setDomStorageEnabled(true);
		mWebView.getSettings().setUseWideViewPort(true);
		mWebView.loadUrl(getIntent().getStringExtra("url"));
		mWebView.setWebViewClient(new WebViewClient() {
			
			public boolean shouldOverrideUrlLoading(WebView view, String url)
			{
				if(url.startsWith("mbspay:")){
					PackageManager pm = getPackageManager();
					Intent checkIntent = pm.getLaunchIntentForPackage("com.chinamworld.main");
					if(checkIntent!=null){//不等于空说明用户安装了建行ＡＰＰ
						Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
						startActivity(intent);
					}else{
						createDialog();
					}
					return true;
				}else{
					return false;
				}  
				
				
			}
			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				// TODO Auto-generated method stub
				super.onReceivedError(view, errorCode, description, failingUrl);
				Toast.makeText(BrowserPay.this, "服务器连接异常，请检查网络是否正常", Toast.LENGTH_SHORT).show();
				mLoadingCover.onRefreshFailed();
			}
			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onPageFinished(view, url);
				mLoadingCover.onRefreshSuccess();
			}
			
		});
		
		
		img = (TextView)findViewById(R.id.img_view);
		img.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});

	}

	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mWebView.destroy();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
			mWebView.goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onLoadingCoverRefresh() {
		// TODO Auto-generated method stub

	}
	@Override
	protected void onResume() {
		/**
		 * 设置为竖屏
		 */
		if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		}
		super.onResume();
	}
	private void createDialog() {
		mMenudialog = new Dialog(this, R.style.DialogTheme);
		View dialogView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.mycommunity_dialog_layout, null);
		TextView mTvTitle = (TextView) dialogView.findViewById(R.id.tv_title);
		mTvTitle.setText("温馨提示：您还未成功安装建设银行手机APP,金额超过1000元您将无法支付,为方便您的生活建议您进行安装!");
		Button btn_change = (Button) dialogView.findViewById(R.id.btn_change);
		btn_change.setText("好的,我知道了");
		btn_change.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mMenudialog.dismiss();
			}
		});
		Button btn_cancel = (Button) dialogView.findViewById(R.id.btn_cancel);
		btn_cancel.setVisibility(View.GONE);
		mMenudialog.setContentView(dialogView);
		mMenudialog.setCancelable(false);
		mMenudialog.setCanceledOnTouchOutside(false);
		mMenudialog.show();
	}

}
